package com.oocl.tm.clientThread;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import com.oocl.tm.log4j.Log4j;
import com.oocl.tm.protocol.MsgPackage;
import com.oocl.tm.protocol.Operation;
import com.oocl.tm.service.ServerService;
import com.oocl.tm.service.impl.ServerServiceImpl;

public class ClientThread extends Thread{
	
	private ServerService service;
	private InputStream in;
	private OutputStream  out;
	
	public ClientThread(Socket client) throws IOException{
		in = client.getInputStream();
		out =client.getOutputStream();
		this.service = new ServerServiceImpl(client, in, out);
	}
	
	
	public synchronized void run(){
		while(true){
			MsgPackage mp = service.getMsgPackage();
			try {
				service.process();
			} catch (IOException e) {
				Log4j.getLogger().warn(e, e.fillInStackTrace());
			}
			if(mp.getOperation().equals(Operation.EXIT)){
				break;
			}
		}
	}
}
